package com.xhs.hawaii.purchase.controller;

import com.xhs.hawaii.common.PageResult;
import com.xhs.hawaii.common.utils.AssertUtils;
import com.xhs.hawaii.framework.UserInfoBag;
import com.xhs.hawaii.purchase.enums.ManualPoEvent;
import com.xhs.hawaii.purchase.enums.PauseEvent;
import com.xhs.hawaii.purchase.model.PurchaseOrderLog;
import com.xhs.hawaii.purchase.service.PurchaseOrderFacade;
import com.xhs.hawaii.purchase.service.PurchaseOrderService;
import com.xhs.hawaii.purchase.vo.*;
import com.xhs.hawaii.purchase.vo.query.ManualPoQuery;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author tangchuang
 * @version 1.0
 * @since 2018/1/23
 */
@RestController
@RequestMapping("/purchase/order")
public class PurchaseOrderController {

    @Autowired
    private PurchaseOrderFacade purchaseOrderFacade;


    @RequestMapping(value = "/queryManualPoVo", method = RequestMethod.POST)
    public PageResult<ManualPoVo> queryManualPoVo(ManualPoQuery manualPoQuery) {
        return purchaseOrderFacade.queryManualPoVo(manualPoQuery);
    }

    /**
     * 采购单详情
     *
     * @param purchaseOrderId
     * @return
     */
    @ApiOperation(value = "/getDetail", notes = "采购单详情")
    @RequestMapping(value = "/getDetail", method = RequestMethod.GET)
    public PurchaseOrderVo getDetail(Long purchaseOrderId) {
        return purchaseOrderFacade.getDetail(purchaseOrderId);
    }

    /**
     * 查询操作日志
     *
     * @param purchaseOrderId
     * @return
     */
    @ApiOperation(value = "/logs", notes = "查询操作日志")
    @RequestMapping(value = "/logs", method = RequestMethod.GET)
    public List<PurchaseOrderLog> getLogsByPurchaseId(Long purchaseOrderId) {
        return purchaseOrderFacade.getLogsByPurchaseId(purchaseOrderId);
    }

    /**
     * 手工单保存新增
     *
     * @param manualAddVo
     */
    @ApiOperation(value = "/manual/add/save", notes = "手工单保存新增")
    @RequestMapping(value = "/manual/add/save", method = RequestMethod.POST)
    public void manualAddSave(ManualPoAddVo manualAddVo) {
        AssertUtils.check(manualAddVo.getPurchaseOrderId() == null, "采购单id需要为null");
        manualAddVo.setOperation(ManualPoEvent.SAVE.name());
        purchaseOrderFacade.serviceManualPo(manualAddVo, UserInfoBag.get());
    }

    /**
     * 手工单提交新增
     *
     * @param manualAddVo
     */
    @ApiOperation(value = "/manual/add/commit", notes = "手工单提交新增")
    @RequestMapping(value = "/manual/add/commit", method = RequestMethod.POST)
    public void manualAddCommit(ManualPoAddVo manualAddVo) {
        manualAddVo.setOperation(ManualPoEvent.COMMIT.name());
        purchaseOrderFacade.serviceManualPo(manualAddVo, UserInfoBag.get());
    }

    /**
     * 手工单保存修改
     *
     * @param baseEventPoVo
     */
    @ApiOperation(value = "/manual/save", notes = "手工单保存修改")
    @RequestMapping(value = "/manual/save", method = RequestMethod.POST)
    public void manualSave(BaseEventPoVo baseEventPoVo) {
        baseEventPoVo.setOperation(ManualPoEvent.SAVE.name());
        purchaseOrderFacade.serviceManualPo(baseEventPoVo, UserInfoBag.get());
    }

    /**
     * 手工单提交修改
     *
     * @param baseEventPoVo
     */
    @ApiOperation(value = "/manual/commit", notes = "手工单提交修改")
    @RequestMapping(value = "/manual/commit", method = RequestMethod.POST)
    public void manualCommit(BaseEventPoVo baseEventPoVo) {
        baseEventPoVo.setOperation(ManualPoEvent.COMMIT.name());
        purchaseOrderFacade.serviceManualPo(baseEventPoVo, UserInfoBag.get());
    }

    /**
     * 手工单审核同意
     *
     * @param baseEventPoVo
     */
    @ApiOperation(value = "/manual/agree", notes = "手工单审核同意")
    @RequestMapping(value = "/manual/agree", method = RequestMethod.POST)
    public void manualAgree(BaseEventPoVo baseEventPoVo) {
        baseEventPoVo.setOperation(ManualPoEvent.AGREE.name());
        purchaseOrderFacade.serviceManualPo(baseEventPoVo, UserInfoBag.get());
    }

    /**
     * 手工单审核拒绝
     *
     * @param baseEventPoVo
     */
    @ApiOperation(value = "/manual/refuse", notes = "手工单审核拒绝")
    @RequestMapping(value = "/manual/refuse", method = RequestMethod.POST)
    public void manualRefuse(BaseEventPoVo baseEventPoVo) {
        baseEventPoVo.setOperation(ManualPoEvent.REFUSE.name());
        purchaseOrderFacade.serviceManualPo(baseEventPoVo, UserInfoBag.get());
    }

    /**
     * 暂停
     */
    @ApiOperation(value = "/pause", notes = "暂停")
    @RequestMapping(value = "/pause", method = RequestMethod.POST)
    public void pause(BaseEventPoVo baseEventPoVo) {
        baseEventPoVo.setOperation(PauseEvent.PAUSE.name());
        purchaseOrderFacade.servicePause(baseEventPoVo, UserInfoBag.get());
    }

    /**
     * 暂停同意
     */
    @ApiOperation(value = "/pause/agree", notes = "同意暂停")
    @RequestMapping(value = "/pause/agree", method = RequestMethod.POST)
    public void pauseAgree(BaseEventPoVo baseEventPoVo) {
        baseEventPoVo.setOperation(PauseEvent.AGREE_PAUSE.name());
        purchaseOrderFacade.servicePause(baseEventPoVo, UserInfoBag.get());
    }

    /**
     * 暂停拒绝
     */
    @ApiOperation(value = "/pause/refuse", notes = "拒绝暂停")
    @RequestMapping(value = "/pause/refuse", method = RequestMethod.POST)
    public void pauseRefuse(BaseEventPoVo baseEventPoVo) {
        baseEventPoVo.setOperation(PauseEvent.REFUSE_PAUSE.name());
        purchaseOrderFacade.servicePause(baseEventPoVo, UserInfoBag.get());
    }

    @Autowired
    private PurchaseOrderService purchaseOrderService;

    @RequestMapping(value = "test/",method = RequestMethod.POST)
    public void receivePurchaseOrderSku(PurchaseOrderReceiveVo purchaseOrderReceiveVo){
        purchaseOrderService.receivePurchaseOrderSku(purchaseOrderReceiveVo);
    }

}
